<?php namespace Admin\Controller;

class GoodsController extends CommonController{
	private $model;
	public function __init(){
		//调用CommonController里面的构造函数
		parent::__init();
		//实例化
	  	$this->model = new \Admin\Model\Goods; 
	}
	//展示
	public function index(){
//		分页
		$totle = $this->model->where("gupdown=0")->count();
		$page = Page::row(5)->make($totle);
		View::with('page',$page);
		View::with('goodsdata',$this->model->where("gupdown=0")->get());
	    View::make();
	}
	
	//添加
	public function add(){
		if(IS_POST){
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('添加成功',U('index'));
		}
	    View::make();
	}
//	Ajax
	public function getTypeAttr(){
		$tid = Q('post.id',0,'intval');
		$data = Db::table('typeattr')->where("type_tid={$tid}")->get();
		foreach($data as $k => $v){
			$data[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		echo json_encode($data);exit;
	}
	
	//下架
	public function down(){
	    //就是把gupdown改成1
	    $this->model->where("gid=" . Q('get.gid',0,'intval'))->save(array('gupdown'=>1));
		View::success('下架成功',U('index'));
	}
	//上架
	public function up(){
	    //就是把gupdown改成0
	    $this->model->where("gid=" . Q('get.gid',0,'intval'))->save(array('gupdown'=>0));
		View::success('上架成功',U('updown'));
	}
	
	//下架列表
	public function updown(){
//	    分页
		$totle = $this->model->where("gupdown=1")->count();
		$page = Page::row(5)->make($totle);
		View::with('page',$page);
		View::with('goodsdata',$this->model->where("gupdown=1")->get());
	    View::make();
	}
//	删除
	public function del(){
		$this->model->where("gid=" . Q('get.gid',0,'intval'))->delete();
		Db::table('goodsdes')->where("goods_gid=" . Q('get.gid',0,'intval'))->delete();
		Db::table('goodsattr')->where("goods_gid=" . Q('get.gid',0,'intval'))->delete();
		Db::table('goodslist')->where("goods_gid=" . Q('get.gid',0,'intval'))->delete();
	    View::success('删除成功',U('index'));
	}
	 
	//编辑
	public function edit(){
		$gid = Q('get.gid',0,'intval');
		$olddata = $this->model->where("gid=$gid")->get();
		View::with('olddata',$olddata);
		
		$typeattr = Db::table('typeattr')->where("type_tid={$olddata[0]['type_tid']} and taclass=1")->get();
		$goodsattr = Db::table('goodsattr')->where("goods_gid={$olddata[0]['gid']} and gaadd!= 'em'")->get();
		$all = Db::table('typeattr')->join('goodsattr','taid','=','typeattr_taid')->where("goods_gid={$olddata[0]['gid']} and taclass=1")->get();
		foreach ($all as $k => $value) {
			$all[$k]['tavalue'] = explode(',', $all[$k]['tavalue']);
			$all[$k]['gavalue'] = explode(',', $all[$k]['gavalue']);
			$all[$k]['gaadd'] = explode(',', $all[$k]['gaadd']);
		}
		View::with('typeattr',$typeattr);
		View::with('goodsattr',$goodsattr);
		View::with('all',$all);
		
		if(IS_POST){
			$this->model->where("gid=" . Q('get.gid',0,'intval'))->delete();
			Db::table('goodsdes')->where("goods_gid=" . Q('get.gid',0,'intval'))->delete();
			Db::table('goodsattr')->where("goods_gid=" . Q('get.gid',0,'intval'))->delete();
			Db::table('goodslist')->where("goods_gid=" . Q('get.gid',0,'intval'))->delete();
			if(!$this->model->store()) View::error($this->model->getError());
			View::success('编辑成功',U('index'));
		}
	    View::make();
	}
	
	 
	//uploadify异步上传----列表图
	public function uploadgpic()
	{
	    $file = Upload::path('Upload/Content/gpic/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
	                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
	                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
	                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	//uploadify异步上传----详细图
	public function uploadgdpic()
	{
	    $file = Upload::path('Upload/Content/gdpic/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
	                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
	                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
	                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	

	
	
	
}
	